package com.chefu.system.controller;


import com.chefu.common.exception.CfException;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.pojo.R;
import com.chefu.system.pojo.Dept;
import com.chefu.system.pojo.Emp;
import com.chefu.system.service.DeptService;
import com.chefu.system.service.EmpService;
import com.chefu.system.vo.EmpVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/system/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private DeptService deptService;

    /**
     * 分页查询员工信息
     *
     * @param page     当前页
     * @param pageSize 页面大小
     * @param name     员工姓名
     * @param phone    手机号码
     * @param email    电子邮件
     * @return
     */
    @GetMapping("/list")
    public R findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "deptId", required = false) Long deptId,
            @RequestParam(value = "status",required = false) Integer status
    ) {
        List<EmpVo> empVoList = empService.findByPage(page, pageSize, name, phone, email, deptId,status);
        return R.ok().data("empVoList", empVoList);
    }

    /**
     * 添加查询功能
     *
     * @return
     */
    @GetMapping("/findAll")
    public R findDept() {
        List<Dept> deptList = deptService.findAll();
        return R.ok().data("deptList", deptList);
    }

    /**
     * 新增员工
     *
     * @param empVo
     * @param bindingResult
     * @return
     */

    @PostMapping("/insert")
    public R insert(@Valid EmpVo empVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //取出错误信息
            String errorMsg = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        empService.insert(empVo);
        return R.ok();
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @GetMapping("/query")
    public R findById(@RequestParam("id") Long id) {
        EmpVo empVo = empService.findById(id);
        return R.ok().data("empVo", empVo);

    }

    /**
     * 修改员工
     * @param emp
     * @param bindingResult
     * @return
     */
    @PutMapping("/update")
    public R update(@Valid Emp emp, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //取出错误信息
            String errorMsg = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
            System.out.println(errorMsg);
            throw new CfException(5000, errorMsg);
        }
        empService.updateDept(emp);
        return R.ok();
    }

    /**
     * 锁定员工
     * @param empId
     * @param status
     * @return
     */
    @PutMapping("/updateStatus")
    private R updateStatus(@RequestParam("empId") Long empId, @RequestParam("status") Integer status) {

        empService.updateStatus(empId, status);
        return R.ok();
    }
}
